package cn.czh.org.controller;

import cn.czh.basic.annotation.PreAuthorize;
import cn.czh.basic.util.AjaxResult;
import cn.czh.basic.util.PageList;
import cn.czh.org.domain.Department;
import cn.czh.org.query.DepartmentQuery;
import cn.czh.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.junit.platform.commons.function.Try;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门接口类
 */
@RestController//@Controller，@ResponseBody的组合注解
@RequestMapping("/department")
@Api(value = "部门接口类",description="部门的CRUD操作")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接口：查询所有
     * @return
     */
    //http://localhost:8080/department - Get
    @GetMapping
    @ApiOperation(value = "查询所有接口")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 接口：查询单个
     * @param id
     * @return
     */
    //http://localhost:8080/department/2 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个接口")
    public Department findOne(@PathVariable("id") Long id){
        return departmentService.findOne(id);
    }

    /**
     * 接口：分页查询
     * @param departmentQuery
     * @return
     */
    //http://localhost:8080/department - Post - 参数通过json对象传递
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    @PreAuthorize(name = "部门列表",sn = "department:list")
    //@RequestBody - 从请求体中获取数据赋值给departmentQuery
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 接口：删除接口
     * @param id
     * @return
     */
    //http://localhost:8080/department/2 - Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    @PreAuthorize(name = "删除部门",sn = "department:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除失败");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("删除失败").setResultObj("重新操作");
        }
    }


    @PatchMapping
    @ApiOperation(value = "批量删接口")
    @PreAuthorize(name = "批量删除部门",sn = "department:patchDel")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除失败");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("删除失败").setResultObj("重新操作");
        }
    }

    /**
     * 接口：添加和修改
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    @PreAuthorize(name = "保存部门",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            //如果id为空
            if (department.getId() == null){
                //就是添加
                departmentService.add(department);
            }else {
                //否则就是修改
                departmentService.update(department);
            }
            //返回一个无参构造
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"操作失败");
            //链式语法
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObj("重新操作");
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门数 -- 无限极接口")
    public List<Department> deptTree(){
        List<Department> departments = departmentService.deptTree();
        System.out.println(departments);
        return departments;
    }
}
